﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Data;
using System.Data.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EffiProz.Core;
using EffiProz.Core.Lib;
using EffiProz.Core.Persist;
using System.Data.EffiProz;
using EffiProz.Core.Server;

namespace EffiProzDBTest.SelfTest
{
    [TestClass]
    public class TestServer 
    {
        string testScriptPath = "../../../EffiProzTest/SelfTest/Scripts/";
        //string serverDataPath = "../../Data/";
        string serverPath = @"C:\Users\irantha\Documents\Visual Studio 2008\Projects\EffiProz\EffiProzServer\bin\Debug\EffiProz.Server.exe";
        Process server;

        private void CleanUp()
        {

        }
        
       
        public void StartServer()
        {
            server = new Process();
            server.StartInfo.FileName = serverPath;
            server.StartInfo.WorkingDirectory = Path.GetDirectoryName(serverPath);           
            server.Start();
            Thread.Sleep(5000);
        }

        public void StartServerSsh()
        {
            server = new Process();
            server.StartInfo.FileName = serverPath;
            server.StartInfo.WorkingDirectory = Path.GetDirectoryName(serverPath);         
            server.StartInfo.Arguments = "EFZS";
            server.Start();
            Thread.Sleep(5000);
        }

        public void StartWebServerSsh()
        {
            server = new Process();
            server.StartInfo.FileName = serverPath;
            server.StartInfo.WorkingDirectory = Path.GetDirectoryName(serverPath);
            server.StartInfo.Arguments = "HTTPS";
            server.Start();
            Thread.Sleep(5000);
        }

        public void StartWebServer()
        {
            server = new Process();
            server.StartInfo.FileName = serverPath;
            server.StartInfo.WorkingDirectory = Path.GetDirectoryName(serverPath);
            server.StartInfo.Arguments = "HTTP";
            server.Start();
            Thread.Sleep(5000);
        }

        public void StopServer()
        {
            try
            {
                server.Kill();
            }
            catch (Exception)
            {
            }
        }

        public TestServer()      
        {
           
        }

        [TestMethod]
        public void ADONetBasicServer()
        {
            lock (this)
            {
                StartServer();

                try
                {
                    string connString = "Connection Type=Server ;Data Source=localhost:9001; Initial Catalog=test; User=sa; Password=;";
                    string sql = "DROP TABLE TEST IF EXISTS; CREATE TABLE TEST(ID INT, NAME VARCHAR(100));";
                    using (EfzConnection conn = new EfzConnection(connString))
                    {
                        DbCommand command = conn.CreateCommand();
                        command.CommandText = sql;

                        conn.Open();
                        int count = command.ExecuteNonQuery();
                        command.CommandText = "INSERT INTO TEST(ID , NAME ) VALUES(1,'irantha'); INSERT INTO TEST(ID , NAME ) VALUES(NULL,'nuwan');";
                        count = command.ExecuteNonQuery();
                        Assert.AreEqual(count, 1);

                        command.CommandText = "SELECT * FROM TEST;";
                        DbDataReader reader = command.ExecuteReader();
                        System.Diagnostics.Debug.WriteLine(reader.FieldCount);
                        while (reader.Read())
                        {
                            if (reader.IsDBNull(0))
                                System.Diagnostics.Debug.WriteLine("ID= " + " Name=" + reader.GetString(1));
                            else
                                System.Diagnostics.Debug.WriteLine("ID= " + reader.GetInt32(0) + " Name=" + reader.GetString(1));
                        }

                        command = conn.CreateCommand();
                        command.CommandText = "Call Sqrt(25);";
                        double sqrt = (double)command.ExecuteScalar();
                        Assert.AreEqual(5, sqrt);
                    }
                }
                finally
                {
                    StopServer();
                }
            }
        }


        [TestMethod]
        public void ADONetBasicServerSSH()
        {
            lock (this)
            {
                StartServerSsh();

                try
                {
                    string connString = "Connection Type=Server ; Ssl=true; Data Source=localhost:9001; Initial Catalog=test; User=sa; Password=;";
                    string sql = "DROP TABLE TEST IF EXISTS; CREATE TABLE TEST(ID INT, NAME VARCHAR(100));";
                    using (EfzConnection conn = new EfzConnection(connString))
                    {
                        DbCommand command = conn.CreateCommand();
                        command.CommandText = sql;

                        conn.Open();
                        int count = command.ExecuteNonQuery();
                        command.CommandText = "INSERT INTO TEST(ID , NAME ) VALUES(1,'irantha'); INSERT INTO TEST(ID , NAME ) VALUES(NULL,'nuwan');";
                        count = command.ExecuteNonQuery();
                        Assert.AreEqual(count, 1);

                        command.CommandText = "SELECT * FROM TEST;";
                        DbDataReader reader = command.ExecuteReader();
                        System.Diagnostics.Debug.WriteLine(reader.FieldCount);
                        while (reader.Read())
                        {
                            if (reader.IsDBNull(0))
                                System.Diagnostics.Debug.WriteLine("ID= " + " Name=" + reader.GetString(1));
                            else
                                System.Diagnostics.Debug.WriteLine("ID= " + reader.GetInt32(0) + " Name=" + reader.GetString(1));
                        }

                        command = conn.CreateCommand();
                        command.CommandText = "Call Sqrt(25);";
                        double sqrt = (double)command.ExecuteScalar();
                        Assert.AreEqual(5, sqrt);
                    }
                }
                finally
                {
                    StopServer();
                }
            }
        }

        [TestMethod]
        public void ADONetBasicWebServerSSH()
        {
            lock (this)
            {
                StartWebServerSsh();

                try
                {
                    string connString = "Connection Type=WebServer ; Ssl=true; Data Source=localhost:80; Initial Catalog=wtest; User=sa; Password=;";
                    string sql = "DROP TABLE TEST IF EXISTS; CREATE TABLE TEST(ID INT, NAME VARCHAR(100));";
                    using (EfzConnection conn = new EfzConnection(connString))
                    {
                        DbCommand command = conn.CreateCommand();
                        command.CommandText = sql;

                        conn.Open();
                        int count = command.ExecuteNonQuery();
                        command.CommandText = "INSERT INTO TEST(ID , NAME ) VALUES(1,'irantha'); INSERT INTO TEST(ID , NAME ) VALUES(NULL,'nuwan');";
                        count = command.ExecuteNonQuery();
                        Assert.AreEqual(count, 1);

                        command.CommandText = "SELECT * FROM TEST;";
                        DbDataReader reader = command.ExecuteReader();
                        System.Diagnostics.Debug.WriteLine(reader.FieldCount);
                        while (reader.Read())
                        {
                            if (reader.IsDBNull(0))
                                System.Diagnostics.Debug.WriteLine("ID= " + " Name=" + reader.GetString(1));
                            else
                                System.Diagnostics.Debug.WriteLine("ID= " + reader.GetInt32(0) + " Name=" + reader.GetString(1));
                        }

                        command = conn.CreateCommand();
                        command.CommandText = "Call Sqrt(25);";
                        double sqrt = (double)command.ExecuteScalar();
                        Assert.AreEqual(5, sqrt);
                    }
                }
                finally
                {
                    StopServer();
                }
            }
        }

        [TestMethod]
        public void ADONetBasicWebServer()
        {
            lock (this)
            {
                StartWebServer();
                try
                {
                    string connString = "Connection Type=WebServer ;Data Source=localhost:80; Initial Catalog=wtest; User=sa; Password=;";
                    string sql = "DROP TABLE TEST IF EXISTS; CREATE TABLE TEST(ID INT, NAME VARCHAR(100));";
                    using (EfzConnection conn = new EfzConnection(connString))
                    {
                        DbCommand command = conn.CreateCommand();
                        command.CommandText = sql;

                        conn.Open();
                        int count = command.ExecuteNonQuery();
                        command.CommandText = "INSERT INTO TEST(ID , NAME ) VALUES(1,'irantha'); INSERT INTO TEST(ID , NAME ) VALUES(NULL,'nuwan');";
                        count = command.ExecuteNonQuery();
                        Assert.AreEqual(count, 1);

                        command.CommandText = "SELECT * FROM TEST;";
                        DbDataReader reader = command.ExecuteReader();
                        System.Diagnostics.Debug.WriteLine(reader.FieldCount);
                        while (reader.Read())
                        {
                            if (reader.IsDBNull(0))
                                System.Diagnostics.Debug.WriteLine("ID= " + " Name=" + reader.GetString(1));
                            else
                                System.Diagnostics.Debug.WriteLine("ID= " + reader.GetInt32(0) + " Name=" + reader.GetString(1));
                        }

                        command = conn.CreateCommand();
                        command.CommandText = "Call Sqrt(25);";
                        double sqrt = (double)command.ExecuteScalar();
                        Assert.AreEqual(5, sqrt);
                    }
                }
                finally
                {
                    StopServer();
                }
            }
        }

        //[TestMethod]
        //public void WebServerTestSelf()
        //{
        //    lock (this)
        //    {
        //        StartWebServer();

        //        try
        //        {
        //            SessionInterface sessionProxy = new ClientConnectionHTTP("localhost",
        //                80, "wtestself", "wtestself", false, "sa", "", 0);
        //            string scriptPath = testScriptPath + "TestSelf.txt";
        //            EffiProz.Test.SelfTest.TestUtil.testScript(sessionProxy, scriptPath);
        //            sessionProxy.close();
        //        }
        //        finally
        //        {
        //            StopServer();
        //        }
        //    }
        //}


        //[TestMethod]
        //public void WebServerTestSelfAlterTable()
        //{
        //    lock (this)
        //    {
             
        //        StartWebServer();

        //        SessionInterface sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
        //        string scriptPath = testScriptPath + "TestSelfAlterTable.txt";
        //        TestUtil.testScript(sessionProxy, scriptPath);
        //        sessionProxy.close();

        //        StopServer();
        //    }
        //}

        //[TestMethod]
        //public void WebServerTestSelfSchemaPersistA()
        //{
        //    lock (this)
        //    {
              
        //        StartWebServer();

        //        SessionInterface sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
        //        string scriptPath = testScriptPath + "TestSelfSchemaPersistA1.txt";
        //        TestUtil.testScript(sessionProxy, scriptPath);
        //        sessionProxy.close();
        //        Thread.Sleep(5000);

        //        StopServer();

        //        Thread.Sleep(1000);

        //        StartWebServer();

        //        sessionProxy = new HTTPClientConnection("localhost", 80, "", "testdb", false, "sa", "");
        //        scriptPath = testScriptPath + "TestSelfSchemaPersistA2.txt";
        //        TestUtil.testScript(sessionProxy, scriptPath);
        //        sessionProxy.close();


        //        StopServer();

        //    }
        //}


        [TestMethod]
        public void ServerTestSelf()
        {
            lock (this)
            {
                StartServer();

                try
                {
                    SessionInterface sessionProxy = new ClientConnection("localhost",
                        9001, "testself", "testself", false, "sa", "", 0);
                    string scriptPath = testScriptPath + "TestSelf.txt";
                    EffiProz.Test.SelfTest.TestUtil.testScript(sessionProxy, scriptPath);
                    sessionProxy.close();
                }
                finally
                {
                    StopServer();
                }
            }
        }

        [TestMethod]
        public void ServerTestSelfAlterTable()
        {
            lock (this)
            {
                StartServer();

                try
                {
                    SessionInterface sessionProxy = new ClientConnection("localhost",
                        9001, "testselfat", "testselfat", false, "sa", "", 0);
                    string scriptPath = testScriptPath + "TestSelfAlterTable.txt";
                    EffiProz.Test.SelfTest.TestUtil.testScript(sessionProxy, scriptPath);
                    sessionProxy.close();
                }
                finally
                {
                    StopServer();
                }
            }
        }

        [TestMethod]
        public void ServerTestSelfSchemaPersistA()
        {
            lock (this)
            {
                StartServer();

                try
                {

                    SessionInterface sessionProxy = new ClientConnection("localhost",
                        9001, "testselfspa", "testselfspa", false, "sa", "", 0);
                    string scriptPath = testScriptPath + "TestSelfSchemaPersistA1.txt";
                    EffiProz.Test.SelfTest.TestUtil.testScript(sessionProxy, scriptPath);
                    sessionProxy.close();
                    Thread.Sleep(5000);

                    StopServer();

                    Thread.Sleep(1000);

                    StartServer();

                    sessionProxy = new ClientConnection("localhost",
                        9001, "testselfspa", "testselfspa", false, "sa", "", 0);
                    scriptPath = testScriptPath + "TestSelfSchemaPersistA2.txt";
                    EffiProz.Test.SelfTest.TestUtil.testScript(sessionProxy, scriptPath);
                    sessionProxy.close();

                }
                finally
                {
                    StopServer();
                }

            }
        }
    }
}
